package com.demo.controller;

import com.demo.common.CreateDate;
import com.demo.model.BusUsersAccHistory;
import com.demo.service.TabUsersAccHistoryService;
import lombok.extern.log4j.Log4j2;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.util.*;

@Controller
@Log4j2
public class TabUsersAccHistoryController {

    @Resource
    private TabUsersAccHistoryService service;

    @RequestMapping(value = "tab_users_acc_history_view")
    public String tab_users_acc_history_view(String loginname, HttpServletRequest request) {
        if(null==loginname){
            loginname = "";
        }
        List<BusUsersAccHistory> list = service.searchByPage_his(loginname);
        request.setAttribute("list",list);
        request.setAttribute("loginname",loginname);
        return "index/tab_users_acc_history/view";
    }
    @RequestMapping(value = "excel_his")
    public void excel_his(String select_name, HttpServletResponse response,HttpServletRequest request) throws IOException {
        if(null==select_name){
            select_name = "";
        }
        List<BusUsersAccHistory> list = service.searchByPage_his(select_name);
        exportExcelBook(select_name,response, list);
        return;
    }

    /**
     * 导出数据生成EXCEL方法
     * @param select_name
     * @param response
     * @param list
     * @throws IOException
     */
    public void exportExcelBook(String select_name, HttpServletResponse response,List<BusUsersAccHistory> list)
            throws IOException {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        String ti=CreateDate.getDateTimes()+".xls";
        String fileName = URLDecoder.decode(select_name+"-history-"+ti,"UTF-8");
        //创建Excel工作薄对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建Excel工作表对象
        HSSFSheet sheet = workbook.createSheet();
        sheet.setColumnWidth(0, 8000);
        sheet.setColumnWidth(1, 8000);
        sheet.setColumnWidth(2, 8000);
        sheet.setColumnWidth(3, 2500);
        sheet.setColumnWidth(4, 3000);
        sheet.setColumnWidth(5, 5000);
        sheet.setColumnWidth(7, 3000);
        sheet.setColumnWidth(6, 6000);
        sheet.setColumnWidth(8, 4000);

        // 设置表头字体样式
        HSSFFont columnHeadFont = workbook.createFont();
        columnHeadFont.setFontName("宋体");
        columnHeadFont.setFontHeightInPoints((short) 10);
//        columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

        // 列头的样式
        HSSFCellStyle columnHeadStyle = workbook.createCellStyle();
        columnHeadStyle.setFont(columnHeadFont);
        // 左右居中
//        columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//        // 上下居中
//        columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//        columnHeadStyle.setLocked(true);
//        columnHeadStyle.setWrapText(true);
//        // 左边框的颜色
//        columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);
//        // 边框的大小
//        columnHeadStyle.setBorderLeft(BorderStyle.valueOf((short) 1));
//        // 右边框的颜色
//        columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);
//        // 边框的大小
//        columnHeadStyle.setBorderRight(BorderStyle.valueOf((short) 1));
//        // 设置单元格的边框为粗体
//        columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//        // 设置单元格的边框颜色
//        columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index);
//        // 设置单元格的背景颜色（单元格的样式会覆盖列或行的样式）
//        columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);
//        // 设置普通单元格字体样式
        HSSFFont font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 10);

        //创建Excel工作表第一行
        HSSFRow row0 = sheet.createRow(0);
        // 设置行高
        row0.setHeight((short) 750);
        HSSFCell cell = row0.createCell(0);
        //设置单元格内容
        cell.setCellValue(new HSSFRichTextString("用户id"));
        //设置单元格字体样式
        cell.setCellStyle(columnHeadStyle);
        cell = row0.createCell(1);
        cell.setCellValue(new HSSFRichTextString("账户id"));
        cell.setCellStyle(columnHeadStyle);
        cell = row0.createCell(2);
        cell.setCellValue(new HSSFRichTextString("记录id"));
        cell.setCellStyle(columnHeadStyle);
        cell = row0.createCell(3);
        cell.setCellValue(new HSSFRichTextString("登录名"));
        cell.setCellStyle(columnHeadStyle);
        cell = row0.createCell(4);
        cell.setCellValue(new HSSFRichTextString("真实名"));
        cell.setCellStyle(columnHeadStyle);
        cell = row0.createCell(5);
        cell.setCellValue(new HSSFRichTextString("电话"));
        cell.setCellStyle(columnHeadStyle);
        cell = row0.createCell(6);
        cell.setCellValue(new HSSFRichTextString("操作时间"));
        cell.setCellStyle(columnHeadStyle);
        cell = row0.createCell(7);
        cell.setCellValue(new HSSFRichTextString("操作"));
        cell.setCellStyle(columnHeadStyle);
        cell = row0.createCell(8);
        cell.setCellValue(new HSSFRichTextString("操作金额"));
        cell.setCellStyle(columnHeadStyle);

        // 循环写入数据
        for (int i = 0; i < list.size(); i++) {
            BusUsersAccHistory stu = list.get(i);
            HSSFRow row = sheet.createRow(i + 1);
            cell = row.createCell(0);
            cell.setCellValue(new HSSFRichTextString(String.valueOf(stu.getUserid())));
            cell.setCellStyle(columnHeadStyle);
            cell = row.createCell(1);
            cell.setCellValue(new HSSFRichTextString(String.valueOf(stu.getAccid())));
            cell.setCellStyle(columnHeadStyle);
            cell = row.createCell(2);
            cell.setCellValue(new HSSFRichTextString(String.valueOf(stu.getHisid())));
            cell.setCellStyle(columnHeadStyle);
            cell = row.createCell(3);
            cell.setCellValue(new HSSFRichTextString(String.valueOf(stu.getLoginname())));
            cell.setCellStyle(columnHeadStyle);
            cell = row.createCell(4);
            cell.setCellValue(new HSSFRichTextString(String.valueOf(stu.getUsername())));
            cell.setCellStyle(columnHeadStyle);
            cell = row.createCell(5);
            cell.setCellValue(new HSSFRichTextString(String.valueOf(stu.getTel())));
            cell.setCellStyle(columnHeadStyle);
            cell = row.createCell(7);
            cell.setCellValue(new HSSFRichTextString(String.valueOf(stu.getWay())));
            cell.setCellStyle(columnHeadStyle);
            cell = row.createCell(6);
            cell.setCellValue(new HSSFRichTextString(String.valueOf(stu.getWaytimes())));
            cell.setCellStyle(columnHeadStyle);
            cell = row.createCell(8);
            cell.setCellValue(new HSSFRichTextString(String.valueOf(stu.getMonnum())));
            cell.setCellStyle(columnHeadStyle);

        }
        // 获取输出流
        OutputStream os = response.getOutputStream();
        // 重置输出流
        response.reset();
        // 设定输出文件头
        response.setHeader("Content-disposition",
                "attachment; filename=" + new String(fileName.getBytes("GB2312"), "8859_1") + ".xls");
        // 定义输出类型
        response.setContentType("application/msexcel");
        workbook.write(os);
        os.close();
        return;
    }

}
